package com.zhiyou.mall.controller;

import com.zhiyou.mall.model.SGoods;
import com.zhiyou.mall.model.SGoodsDetail;
import com.zhiyou.mall.service.IAdminGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author by zhouyanchao
 * Created on 2017/11/4.
 * Descr:
 */
@Controller
@RequestMapping("/goods")
public class AdminGoodsController {

	final String uploadDir="D:\\60projects\\upload\\";//上传图片的路径
	final String imgServer = "http://localhost:8081/img/";//图片服务器路径

	@Autowired
	IAdminGoodsService goodsService;

	@RequestMapping("index")
	public String index(Model model){

		//查询所有商品信息，这里省略分页
		List<SGoods> goodsList = goodsService.queryAllGoods();

		model.addAttribute("results",goodsList);

		return "goods/index";
	}

	@RequestMapping("editparam")
	public String editparam(int id,Model model){

		SGoods goods = goodsService.queryGoodsById(id);

		model.addAttribute("title",goods.getTitle());
		model.addAttribute("id",id);
		model.addAttribute("catid",goods.getCatid());

		return "goods/goods_tmpl";
	}

	@ResponseBody
	@RequestMapping("uparam")
	public Object uparam(int id,int tmplid,String params){
		System.out.println("商品id:"+id);
		System.out.println("模板id:"+tmplid);
		System.out.println("模板参数:"+params);

		HashMap map = new HashMap<>();
		map.put("success",true);
		return map;
	}

	@RequestMapping("{id}/detail")
	public String detail(@PathVariable int id,Model model){

		SGoods goods = goodsService.queryGoodsById(id);
		//查详情
		SGoodsDetail detail = goodsService.queryGoodsDetailByGoodsUuid(goods.getUuid());
		if(detail==null){
			detail = new SGoodsDetail();
		}

		model.addAttribute("goods",goods);
		model.addAttribute("detail",detail);

		return "goods/detail";
	}

	@RequestMapping(value="{id}/updatedetail",method = RequestMethod.POST)
	public String updatedetail(@PathVariable int id,String detail,Model model){

		System.out.println(detail);

		SGoods goods = goodsService.queryGoodsById(id);
		SGoodsDetail goodsDetail = goodsService.queryGoodsDetailByGoodsUuid(goods.getUuid());
		if(goodsDetail==null){
			goodsDetail = new SGoodsDetail();
			goodsDetail.setGoodsUuid(goods.getUuid());
			goodsDetail.setInsertTime(new Date());
			goodsDetail.setUpdateTime(new Date());
			goodsDetail.setDetail(detail);
			goodsService.insertGoodsDetail(goodsDetail);
		}else{
			goodsDetail.setDetail(detail);
			goodsService.updateGoodsDetail(goodsDetail);
		}


		return "redirect:/goods/index.do";
	}

	/**
	 * Created by zhouyanchao on 2017/11/4
	 * Descr: 上传文件
	 * 返回数据格式
	 * {
	    "success": true/false,
	    "msg": "error message", # optional
	    "file_path": "[real file path]"
	    }
	 *
	 */
	@ResponseBody
	@RequestMapping("upload")
	public Object uploadImg(MultipartFile file){
		HashMap map = new HashMap();
		String oname = file.getOriginalFilename();
		String fname = System.currentTimeMillis()+oname.substring(oname.indexOf("."));
		try {
			file.transferTo(new File(uploadDir + fname));
			map.put("success",true);
			map.put("file_path",imgServer+fname);
		}catch (Exception e){
			e.printStackTrace();
			map.put("success",false);
			map.put("msg","上传文件失败了");
		}

		return map;
	}



}
